package org.hongbo.springbootebsapi.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hongbo.springbootebsapi.annotations.AllowRespInfoNull;
import org.hongbo.springbootebsapi.annotations.ResponseResult;
import org.hongbo.springbootebsapi.entity.AuthUser;
import org.hongbo.springbootebsapi.service.login.ICurrentUser;
import org.hongbo.springbootebsapi.service.login.ILoginService;
import org.hongbo.springbootebsapi.service.responsibility.IResponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Controller
@RestController
@Api(value = "/Responsibility", description = "用户职责的相关API")
@Slf4j
public class ResponController {

    @Autowired
    private IResponService responService;

    @Autowired
    private ILoginService loginService;

    @Autowired
    protected JdbcTemplate jdbcTemplate;

    @Autowired
    private ICurrentUser currentUser;


    @ApiOperation(value="获取用户功能列表", notes="获取用户功能列表")
    @GetMapping("/getUserAllFunction")
    @AllowRespInfoNull
    public String getUserAllFunction(){
        String userFuncStr = null;
        log.info("getUserAllFunction currentUser "+ JSON.toJSONString(currentUser.getCurrentUser()));
        String userId = currentUser.getCurrentUserId();

        long startTime = System.currentTimeMillis();
        userFuncStr = responService.getUserFuncStr(userId);
        log.info("生成用户职责列表耗时 "+(System.currentTimeMillis()-startTime)+"ms");

        return userFuncStr;

    }


    @GetMapping("/responsibility/loginuser")
    @ResponseResult
    @AllowRespInfoNull
    public AuthUser loginUser(){
        log.info("GetMapping  /responsibility/loginuser");
        return currentUser.getCurrentUser();
    }


    @GetMapping("/responsibility/userinfo")
    @ResponseResult
    public String userInfo(){
        return responService.getUserInfo(currentUser.getCurrentUser());
    }

    @GetMapping("/responsibility/logininfo")
    @ResponseResult
    public String logininfo(){
        return responService.getLoginInfo();
    }


    @ApiOperation(value="切换职责", notes="切换职责刷新token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "respId", value = "职责ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "respApplId", value = "职责应用ID", required = true, dataType = "String")
    })
    @GetMapping("/responsibility/switcher")
    @ResponseResult
    @AllowRespInfoNull
    public String respSwitcher(@RequestParam(value = "respId") String respId,
                               @RequestParam(value = "respApplId") String respApplId){
        return responService.getRespSwitcherToken(respId, respApplId);
    }



}
